<%--
  Created by IntelliJ IDEA.
  User: 61741
  Date: 2018/1/20
  Time: 16:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<style>
    table img.data-preview-img.edit_able {
        margin: 0.2rem 0.4rem;
        width: 8rem;
    }

    span.edit_able {
        cursor: pointer;
        position: relative;
        border-bottom: 1px solid transparent;
    }

    span.edit_able:hover {
        border-bottom-color: #8c8c8c;

    }

    span.edit_able:hover:after {
        content: '\f044';
        font-family: FontAwesome;
        margin-left: 0.5rem;
        color: #0aa;
        text-decoration: none !important;

    }
</style>
<script id="table-tml" type="text/html">
    <tr>
        <td>
            <span>\${location_id}</span>
        </td>
        <td>
            <div class="form-group">
                <img id="img-preview\${location_id}" name="img-preview" class="img_upload_preview"
                     for="logoimg-file\${location_id}" data-target="img-val" src="\${img}"
                     onclick=$("#logoimg-file\${location_id}").click()
                     onerror="this.src='${ctx}/resources/ueditor/dialogs/image/images/image.png'"/>
                <input id="logoimg-file\${location_id}" type="file" name="logoimg-file" style="display: none;"
                       onchange="uploadFile('logoimg-file\${location_id}','img-preview\${location_id}','good_pic\${location_id}','image')"/>
                <input name="good_pic" id='good_pic\${location_id}' type="hidden">
            </div>
        </td>
        <td>
            <div class="col-md-6" id="newgoodSelectBox">
                <select id="newgoodShopSelect" data-target="goodSelect\${location_id}" onchange="showShopGood(this)">
                    <option value="\${userId}">\${userName}</option>
                </select>
                <select id="goodSelect\${location_id}">
                    <option value="\${goodId}">\${goodName}</option>
                </select>
            </div>
        </td>
        <td>
            <button class="btn btn-danger" onclick="sbmitMod('\${_id}','\${location_id}')">提交</button>
        </td>
    </tr>
</script>
<script type="text/javascript">
    $(function () {
        loadNewGood();
        loadShop();
    })

    function loadNewGood() {
        var data = {city: "default"};
        asyncAjax({
            url: ctx + "/portal/newgoodinfo/list?" + $.param(data).replace(/(%5D|%5B)/g, ""),
            beforeSend: function (req) {
                $("#newGood-table tbody").html('<i class="fa fa-spin fa-spinner"></i>正在刷新数据...');
            },
            success: function (res) {
                $("#newGood-table tbody").html('');
                //console.log("res:%o", res);
                if (res["result"] == "0") {
                    if (!isEmpty(res["data"])) {
                        $("#table-tml").tmpl(res["data"]).appendTo($("#newGood-table tbody"));
                    }
                    else {
                        $("#newGood-table").html('<div class="alert alert-warning">该模块尚未配置任何信息!</div>');
                    }
                } else {
                    bootbox.alert(res.msg);
                }
            }
        })
    }

    function editCallBack(data, el) {
        updateSetinfo(data, el);
    }

    function updateSetinfo(data, el) {
        var param = {
            id: data["id"],
            name:data["name"],
            src:data["src"],
            value:data["value"]

        };
        updateNewGoodSet(param, function () {
            if (data["type"] == "text" || data["type"] == "time") {
                $(el).text(data["value"]);
            } else if (data["type"] == "img") {
                $(el).attr("src", data["value"]);
            } else if (data["type"] == "good-class") {
                //console.log("update class:%o,el:%o", data, el);
                if (el) {
                    $(el).html(data["value"]);
                }
            }
        })
    }

    function updateNewGoodSet(data, _callback) {
        asyncAjax({
            url: ctx + "/portal/newgoodinfo/update",
            data:data,
            type: "post",
            success: function (res) {
                //console.log("update good res:%o", res);
                if (res.result == "0" || res.state == "SUCCESS") {
                    bootbox.alert("修改成功");
                    if (typeof _callback == "function") {
                        _callback();
                    }
                }
            }
        })

    }
    function loadShop() {
        var data = {reviewStatus: 2};
        asyncAjax({
            url: ctx + "/user/mgr/list/99?" + $.param(data).replace(/(%5D|%5B)/g, ""),//行业打牌默认配置厂商,
            beforeSend:function(){
                $("#shopSelect,#goodMerchantSelector,#carouselShopSelect,#newgoodShopSelect,#artShopSelector").html('');
            },
            success: function (res) {
                //console.log("res:%o", res);
                if (res["result"] == "0") {
                    $.each(res["data"], function (idx, shopUser) {
                        $("#shopSelect,#goodMerchantSelector,#carouselShopSelect,#newgoodShopSelect,#artShopSelector").append("<option value='" + shopUser.id + "'>" + shopUser.companyName + "</option>");
                    })
                    $("#shopSelect,#goodMerchantSelector,#carouselShopSelect,#newgoodShopSelect,#artShopSelector").chosen({width: 380});
                } else {
                    bootbox.alert(res["msg"]);
                }
            }
        })
    }
    function updateinfo(id){
        var src=$("#goodSelect").val();
        var data ={id:id ,name:"good_id",src:"",value:src};
        asyncAjax({
            url: ctx + "/portal/newgoodinfo/update?"+$.param(data).replace(/(%5D|%5B)/g, ""),
            type: "post",
            success: function (res) {
                //console.log("update good res:%o", res);
                if (res.result == "0" || res.state == "SUCCESS") {
                    bootbox.alert("修改成功");
                }
            }
        })
    }
    function sbmitMod(id,locationId) {
        var target = $("#newgoodShopSelect").attr("data-target");
        var userId =$("#newgoodShopSelect").val();
        var userName =$("#newgoodShopSelect").find("option:selected").text();
        var goodId=  $("#" + target).val();
        var goodName= $("#" + target).find("option:selected").text();
        var img =$("#good_pic"+locationId).val();
        var data ={ id:id,userId:userId,userName:userName,goodId:goodId,goodName:goodName,img:img}
        asyncAjax({
            url: ctx + "/portal/newgoodinfo/update?"+$.param(data).replace(/(%5D|%5B)/g, ""),
            type: "post",
            success: function (res) {
                //console.log("update good res:%o", res);
                if (res.result == "0" || res.state == "SUCCESS") {
                    bootbox.alert("修改成功");
                }
                else {
                    bootbox.alert(res["msg"]);
                }
            }
        })
    }
</script>
